package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.annotation.TingShuCache;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {
    @Resource
    private BaseCategoryViewMapper baseCategoryViewMapper;
    @Resource
    private BaseAttributeMapper baseAttributeMapper;
    @Resource
    private BaseAttributeValueMapper baseAttributeValueMapper;
    @Resource
    private BaseCategory2Mapper baseCategory2Mapper;
    @Resource
    private BaseCategory3Mapper baseCategory3Mapper;


    @Override
    public List<JSONObject> getBaseCategoryList() {
        List<JSONObject> categories1 = new ArrayList<>();
        List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);
        if (CollectionUtils.isEmpty(baseCategoryViews)) {
            return null;
        }

        Map<Long, List<BaseCategoryView>> baseCategory1Map = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        baseCategory1Map.forEach((category1Id, category2List) -> {
            JSONObject jsonObject1 = getJsonObject(category1Id, category2List);
            categories1.add(jsonObject1);
        });
        return categories1;
    }

    @Override
    public List<BaseAttribute> findAttribute(Long category1Id) {
        List<BaseAttribute> baseAttributes = baseAttributeMapper.selectList(new LambdaQueryWrapper<BaseAttribute>().eq(BaseAttribute::getCategory1Id, category1Id));
        if (CollectionUtils.isEmpty(baseAttributes)) return null;
        baseAttributes.forEach(baseAttribute -> {
            List<BaseAttributeValue> baseAttributeValues = baseAttributeValueMapper.selectList(new LambdaQueryWrapper<BaseAttributeValue>().eq(BaseAttributeValue::getAttributeId, baseAttribute.getId()));
            baseAttribute.setAttributeValueList(baseAttributeValues);
        });
        return baseAttributes;
    }

    @Override
    @TingShuCache(cachePrefix = RedisConstant.CATEGORY_INFO_PREFIX,
            bloomFilter = RedisConstant.ALBUM_BLOOM_FILTER,
            lockPrefix = RedisConstant.CATEGORY_LOCK_PREFIX)
    public BaseCategoryView findBaseCategoryView(Long category3Id) {
        return baseCategoryViewMapper.selectOne(new LambdaQueryWrapper<BaseCategoryView>().eq(BaseCategoryView::getCategory3Id, category3Id));
    }

    @Override
    public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
        List<BaseCategory2> baseCategory2s = baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>().eq(BaseCategory2::getCategory1Id, category1Id));
        if (CollectionUtils.isEmpty(baseCategory2s)) return null;
        List<Long> category2Ids = baseCategory2s.stream().map(BaseCategory2::getId).toList();
        return baseCategory3Mapper.selectList(new LambdaQueryWrapper<BaseCategory3>().eq(BaseCategory3::getIsTop, 1).in(BaseCategory3::getCategory2Id, category2Ids).last("limit 7").select(BaseCategory3::getId, BaseCategory3::getName));
    }

    @Override
    public JSONObject getBaseCategoryList(Long category1Id) {
        List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(new LambdaQueryWrapper<BaseCategoryView>().eq(BaseCategoryView::getCategory1Id, category1Id));
        if (CollectionUtils.isEmpty(baseCategoryViews)) return null;
        return getJsonObject(category1Id, baseCategoryViews);
    }

    @NotNull    ///根据一级分类id查询二级分类及三级分类
    private static JSONObject getJsonObject(Long category1Id, List<BaseCategoryView> category2List) {
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("categoryId", category1Id);
        jsonObject1.put("categoryName", category2List.get(0).getCategory1Name());
        List<JSONObject> categories2 = new ArrayList<>();

        Map<Long, List<BaseCategoryView>> baseCategory2Map = category2List.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
        baseCategory2Map.forEach((category2Id, category3List) -> {
            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("categoryId", category2Id);
            jsonObject2.put("categoryName", category3List.get(0).getCategory2Name());
            jsonObject2.put("categoryChild", category3List.stream().map(aseCategoryView -> {
                JSONObject jsonObject3 = new JSONObject();
                jsonObject3.put("categoryId", aseCategoryView.getCategory3Id());
                jsonObject3.put("categoryName", aseCategoryView.getCategory3Name());
                return jsonObject3;
            }).collect(Collectors.toList()));
            categories2.add(jsonObject2);
        });
        jsonObject1.put("categoryChild", categories2);
        return jsonObject1;
    }
}

